package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.service.Impl.CourseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/course")
public class CourseServlet extends BaseServlet {
    public void findCourseList (HttpServletRequest req,HttpServletResponse resp){


        try {
            CourseService cs = new CourseServiceImpl();
            List<com.lagou.pojo.Course> courseList = cs.findCourseList();
            String s = JSON.toJSONString(courseList);
            resp.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void findCourseNameAndStatus(HttpServletRequest req,HttpServletResponse resp){


        try {
            String course_name = req.getParameter("course_name");
            String status = req.getParameter("status");
            CourseService courseService = new CourseServiceImpl();
            List<Course> result = courseService.findCourseNameAndStatus(course_name, status);

            SimplePropertyPreFilter filter =
                    new SimplePropertyPreFilter(Course.class,"id","course_name","price","sort_num","status");

            String s = JSON.toJSONString(result);
            resp.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void findCourseById(HttpServletRequest req,HttpServletResponse resp){


        try {
            String id = req.getParameter("id");
            int i = Integer.parseInt(id);
            CourseService cs = new CourseServiceImpl();
            List<Course> list = cs.findCourseById(i);

            String s = JSON.toJSONString(list);
            resp.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
